package com.shiyingzi.auser.service.impl;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.shiyingzi.auser.dao.AUserDao;
import com.shiyingzi.auser.model.AUser;
import com.shiyingzi.auser.service.AUserService;
import com.shiyingzi.common.Constants;
import com.shiyingzi.common.EntityUtils;
import com.shiyingzi.common.PageUtils;
import com.shiyingzi.common.PageableData;
import com.shiyingzi.common.ResponseData;
import com.shiyingzi.utils.JdbcTemplateUtils;
import com.shiyingzi.utils.MDFiveUtils;


@Service(AUserService.BEAN_ID)
public class AUserServiceImpl extends ServiceImpl<AUserDao,AUser>  implements AUserService{
	@Autowired
	private AUserDao aUserDao;
	@Autowired
	private HttpSession session;
	@Override
	public ResponseData addAUser(AUser code) {
		try{
			code.setDeleteFlag("N");
			code.setUsPassword(MDFiveUtils.encrypt(code.getUsPassword()));
			EntityUtils.initEntity(code, session);
			
			return new ResponseData(true,"true",insert(code));
		}catch(Exception e){
			return new ResponseData(false,"false");
		}
	}

	@Override
	public PageableData<AUser> queryAUserList(int page, int limit,
			String usName) {
		Page<AUser> mypage = new Page<AUser>();
		mypage.setCurrent(page);
		mypage.setSize(limit);
		AUser ac =new AUser();
		if(!StringUtils.isEmpty(usName)){
			ac.setUsName(usName);
		}
		return PageUtils.tableDataDto(selectPage(mypage,new EntityWrapper<AUser>(ac).orderBy("createTime")));
	}

	@Override
	public ResponseData doALogin(AUser auser) {
		AUser auserDB = aUserDao.selectOne(auser);
		if(auserDB == null){
			return new ResponseData(false, "用户不存在");
		}else if(auserDB.getUsState().equals("3")){
			return new ResponseData(false, "用户未审核");
		}else if(auserDB.getUsState().equals("2")){
			return new ResponseData(false, "用户已停用");
		}else{
			if(MDFiveUtils.encrypt(StringUtils.trim(auser.getUsPassword())).equals(auserDB.getUsPassword())){
				session.setAttribute(Constants.SESSION_USER, auserDB);
				return new ResponseData(true, "登录成功");
			}else{
				return new ResponseData(false, "用户名密码错误");
			}
		}
	}

	@Override
	public AUser getProductById(Integer id) {
		AUser code = selectById(id);
		return code;
	}

//	@Override
//	public ResponseData updateAUser(AUser auser) {
//		try{
//			String sql = "update _a_user set us_phone = '";
//			sql += auser.getUsPhone() + "',";
//			sql += "us_name = '";
//			sql += auser.getUsName() + "',";
//			sql += "us_id_number = '";
//			sql += auser.getUsIdNumber() + "' ";
//			sql += "us_channel = '";
//			sql += auser.getUsChannel() + "' ";
//			sql += "us_home_addres = '";
//			sql += auser.getUsHomeAddres() + "' ";
//			sql += "us_state = '";
//			sql += auser.getUsState() + "' ";
//			sql += "where id = '";
//			sql += auser.getId() + "'";
//			return new ResponseData(true,"true",JdbcTemplateUtils.getTemplate().update(sql));
//		}catch(Exception e){
//			return new ResponseData(false,"false");
//		}
//	}
	@Override
	public ResponseData updateAUser(AUser auser) {
		try{
			EntityUtils.modifyEntity(auser, session);
			return new ResponseData(true,"true",updateById(auser));
		}catch(Exception e){
			return new ResponseData(false,"false");
		}
	}
	@Override
	public ResponseData upPasswordById(AUser auser) {
		try{
			String sql = "update _a_user set us_password = '";
			sql += MDFiveUtils.encrypt(auser.getUsPassword())+ "',";
			sql += "where id = '";
			sql += auser.getId() + "'";
			return new ResponseData(true,"true",JdbcTemplateUtils.getTemplate().update(sql));
		}catch(Exception e){
			return new ResponseData(false,"false");
		}
	}


}
